﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.Database;
namespace smallDeimos
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.Attributes.Add("onchange", "Update()");
            DropDownList2.Attributes.Add("onchange", "Update()");
            DropDownList3.Attributes.Add("onchange", "Update()");
            DropDownList4.Attributes.Add("onchange", "Update()");
            DropDownList5.Attributes.Add("onchange", "Update()");
            DropDownList6.Attributes.Add("onchange", "Update()");
            DropDownList7.Attributes.Add("onchange", "Update()");
            SUbonus.Attributes.Add("onchange", "Update()");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            {
                Label1.Text = "You must provide user name";
                return;
            }
            if (TextBox2.Text == "")
            {
                Label1.Text = "You must provide password";
                return;
            }
            if (TextBox3.Text == "")
            {
                Label1.Text = "You must provide valid email";
                return;
            }
            if (TextBox1.Text.Trim().ToLower().Equals("admin") == true)
            {
                Label1.Text = "Cannot create user.";
                return;
            }
            if (UserFunctions.UserExists(TextBox1.Text.Trim()) == true)
            {
                Label1.Text = "User exists. Please select diffrent name.";
                return;
            }
            else
            {
                int t = 0;
                if (SUbonus.Checked == true)
                    t = 2;

                if (int.Parse(DropDownList2.Text) + int.Parse(DropDownList1.Text) +
                    int.Parse(DropDownList3.Text) + int.Parse(DropDownList5.Text) + int.Parse(DropDownList4.Text) +
                    int.Parse(DropDownList7.Text) + int.Parse(DropDownList6.Text) + t == 0)
                {
                    try
                    {
                        StoredProcedures.CreateUser(TextBox1.Text, TextBox3.Text, TextBox2.Text, int.Parse(DropDownList2.Text), int.Parse(DropDownList1.Text),
                            int.Parse(DropDownList3.Text), int.Parse(DropDownList5.Text), int.Parse(DropDownList4.Text),
                            int.Parse(DropDownList7.Text), int.Parse(DropDownList6.Text), SUbonus.Checked);
                        Label1.Text = "You may now login";
                        LinkButton1.Visible = true;
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.ToString());
                    }
                }
                else
                {
                    Label1.Text = "The sum of race attribs is not 0. You cannot create user.";
                }
            }
        }
    }
}
